package com.sc.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.sc.dto.AttendanceRecordsDTO;
import com.sc.dto.AttendanceRecordsInsertDTO;
import com.sc.dto.AttendanceRecordsUpdateDTO;
import com.sc.dto.BeginAttendanceDTO;
import com.sc.entity.AttendanceRecords;
import com.sc.service.AttendanceRecordsService;
import com.sc.service.StudentService;
import com.sc.vo.AttendanceRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * 考勤记录表 控制层。
 *
 * @author JoeZhou
 * @since 2024-09-14
 */
@RestController
@RequestMapping("/api/v1/attendanceRecords")
public class AttendanceRecordsController {

    @Autowired
    private AttendanceRecordsService attendanceRecordsService;

    /**
     * 添加考勤记录表。
     *
     * @param attendanceRecordsInsertDTO 考勤记录表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public boolean save(@RequestBody @Validated AttendanceRecordsInsertDTO attendanceRecordsInsertDTO) {
        AttendanceRecords attendanceRecords = BeanUtil.copyProperties(attendanceRecordsInsertDTO, AttendanceRecords.class);
        return attendanceRecordsService.save(attendanceRecords);
    }

    /**
     * 添加默认考勤记录
     *
     * @param dto 包含了课程ID和排课ID
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("beginAttendance")
    public Page<AttendanceRecordVo> beginAttendance(@RequestBody BeginAttendanceDTO dto) {
        attendanceRecordsService.beginAttendance(dto);
        Page page = Page.of(dto.getPageNum(), dto.getPageSize());
        return attendanceRecordsService.list(page, dto.getScheduleId());
    }

    /**
     * 根据主键删除考勤记录表。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return attendanceRecordsService.removeById(id);
    }

    /**
     * 根据主键更新考勤记录表。
     *
     * @param attendanceRecordsUpdateDTO 考勤记录表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public boolean update(@RequestBody AttendanceRecordsUpdateDTO attendanceRecordsUpdateDTO) {
        AttendanceRecords attendanceRecords = BeanUtil.copyProperties(attendanceRecordsUpdateDTO, AttendanceRecords.class);
        return attendanceRecordsService.updateById(attendanceRecords,true);
    }

    /**
     * 批量更新考勤记录表。
     *
     * @param dtoList 1条或多条考勤记录
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("updateBatch")
    public boolean updateBatch(@RequestBody List<AttendanceRecordsUpdateDTO> dtoList) {
        List<AttendanceRecords> list = BeanUtil.copyToList(dtoList, AttendanceRecords.class);
        return attendanceRecordsService.updateBatch(list);
    }

    /**
     * 查询所有考勤记录表。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<AttendanceRecordVo> list(@RequestParam Long scheduleId) {
        QueryWrapper queryWrapper = QueryWrapper.create().eq("teaching_schedule_id", scheduleId);
        return attendanceRecordsService.listAs(queryWrapper, AttendanceRecordVo.class);
    }

    /**
     * 根据考勤记录表主键获取详细信息。
     *
     * @param id 考勤记录表主键
     * @return 考勤记录表详情
     */
    @GetMapping("getInfo/{id}")
    public AttendanceRecords getInfo(@PathVariable Serializable id) {
        return attendanceRecordsService.getById(id);
    }

    @Autowired
    private StudentService studentService;
    /**
     * 分页查询考勤记录表。
     *
     * @param attendanceRecordsDTO 分页对象
     * @return 分页对象
     */
    @PostMapping("page")
    public Page<AttendanceRecords>  page(@RequestBody  AttendanceRecordsDTO attendanceRecordsDTO){
        Page<AttendanceRecords> page = new Page<>(attendanceRecordsDTO.getPageNum(),attendanceRecordsDTO.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StrUtil.isNotBlank(attendanceRecordsDTO.getStudentName())) {
            queryWrapper.select("id").like("name", attendanceRecordsDTO.getStudentName());
            List<Long> ids = studentService.listAs(queryWrapper, Long.class);
            //根据学生id查询出勤
            queryWrapper.clear();
            if(CollUtil.isNotEmpty(ids)){
                queryWrapper.in("student_id", ids);
            }
        }
        //根据班级id查询出勤
        if(attendanceRecordsDTO.getClazzId()!=null){

            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("clazz_id",attendanceRecordsDTO.getClazzId()).orderBy("created desc").limit(1);

            AttendanceRecords  attendanceRecords = attendanceRecordsService.getOne(queryWrapper1);

            queryWrapper.where("DATEDIFF(created,'"+attendanceRecords.getCreated()+"')=0 and clazz_id="+attendanceRecords.getClazzId());

        }
        //根据课程id查询出勤
        if(attendanceRecordsDTO.getTeachingScheduleId()!=null){
            queryWrapper.eq("teaching_schedule_id",attendanceRecordsDTO.getTeachingScheduleId());
        }

        return  attendanceRecordsService.pageAs(page,queryWrapper,AttendanceRecords.class);
    }

}
